package com.luchao;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;


public class changeImage {

    /***
     * 功能 :调整图片大小
     */
    public static void resizeImage(String srcPath, String desPath, int scaleSize) throws IOException {

        File srcFile = new File(srcPath);
        Image srcImg = ImageIO.read(srcFile);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(srcFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        float width = bi.getWidth(); // 像素
        float height = bi.getHeight(); // 像素
        float scale=height/scaleSize;
        BufferedImage buffImg = null;
        buffImg = new BufferedImage((int)(width/scale), scaleSize, BufferedImage.TYPE_INT_RGB);
        //使用TYPE_INT_RGB修改的图片会变色
        buffImg.getGraphics().drawImage(
                srcImg.getScaledInstance((int)(width/scale), scaleSize, Image.SCALE_DEFAULT), 0,
                0, null);

        ImageIO.write(buffImg, "JPEG", new File(desPath));


    }

    public static void main(String[] args) {
        try {
            resizeImage("D:\\1.jpg", "D:\\dd.jpg", 300);
        } catch (IOException e) {
            System.out.println("图片转换出现异常！");
        }

    }


}
